//
//  LJYRadioViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/28.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "WJYRadioViewController.h"

@interface WJYRadioViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *collectView;

@property (nonatomic, retain) NSMutableArray *radioTitleArray;
@property (nonatomic, retain) NSMutableArray *radioArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, retain) NSMutableDictionary *radioDic;

@end

@implementation WJYRadioViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrame" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrameBack" object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrameVidioBack" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    // 创建CollectView
    [self createCollectView];
}
#pragma mark --- 创建CollectionView
- (void)createCollectView
{
    
    // collectionView子方法
    UICollectionViewFlowLayout *flowL = [[UICollectionViewFlowLayout alloc] init] ;
    
    // 滚动方向
    flowL.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowL.itemSize = CGSizeMake((self.view.frame.size.width - 4 * 10) * KPropotionWidth / 3, 160 * SIZE );
    
    // 上左下右
    flowL.sectionInset = UIEdgeInsetsMake(15, 10, 10, 10);
    flowL.minimumLineSpacing = 0;
    
    flowL.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
    
    // CollectionView方法
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, self.view.frame.size.height - 64 - 42) collectionViewLayout:flowL];
    
    
    [self.view addSubview:self.collectView];
//    [self.collectView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//    }];
    
    
    
    // 背景颜色
    self.collectView.backgroundColor = [UIColor colorWithRed:0.935 green:0.964 blue:0.948 alpha:1.000];
    
    // 不允许边界反弹
    self.collectView.bounces = NO;
    
    // 代理
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    
    // 注册表头区域
    [self.collectView registerClass:[WJYRadioCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    // 注册cell
    [self.collectView registerClass:[WJYRadioCollectionViewCell class] forCellWithReuseIdentifier:@"WJYRadioCollectionViewCell"];
    
    
    [self.view addSubview:self.collectView];
    [self getDataWithURL:@"http://c.3g.163.com/nc/topicset/ios/radio/index.html"];
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.radioDic.allKeys.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (void)getDataWithURL:(NSString*)url{
    
    self.radioTitleArray = [NSMutableArray array];
    
    self.radioDic = [NSMutableDictionary dictionary];
    
    [AppTools getDataWithURL:url Block:^(id result) {
        
        
        // 将数据存入数组
        NSMutableArray *arr = [result objectForKey:@"cList"];
        
        
        // 遍历数组
        for (NSMutableDictionary *titleDic in arr) {
            
            self.radioArray = [NSMutableArray array];
            
            // 初始化
            WJYRadioModel *radioTitle = [[WJYRadioModel alloc]init];
            
            // 赋值
            radioTitle.cid = [titleDic objectForKey:@"cid"];
            radioTitle.cname = [titleDic objectForKey:@"cname"];
            
            // 存入数组
            [self.radioTitleArray addObject:radioTitle];
            
            // 取出数据存入数组
            NSMutableArray *arr = [titleDic objectForKey:@"tList"];
            
            // key
            self.number++;
            
            // 遍历数组
            for (NSMutableDictionary *titleDic in arr) {
                
                // 初始化
                WJYRadioModel *radio = [[WJYRadioModel alloc]init];
                
                // 赋值
                radio.tid = [titleDic objectForKey:@"tid"];
                radio.playCount = [titleDic objectForKey:@"playCount"];
                
                // 取出数据存入字典
                NSMutableDictionary *tempDic = [titleDic objectForKey:@"radio"];
                
                
                radio.docid = [tempDic objectForKey:@"docid"];
                radio.title = [tempDic objectForKey:@"tname"];
                radio.subTitle = [tempDic objectForKey:@"title"];
                radio.image = [tempDic objectForKey:@"imgsrc"];
                
                // 添加进数组
                [self.radioArray addObject:radio];
                
                // 装进字典
                [self.radioDic setObject:self.radioArray forKey:[NSString stringWithFormat:@"%ld", self.number]];
                
                
            }
            
            
            
        }
        
        
        // 重载界面
        [self.collectView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


#pragma mark CollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WJYRadioCollectionViewCell *radioCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WJYRadioCollectionViewCell" forIndexPath:indexPath];
    
    NSMutableArray *array = [self.radioDic objectForKey:[NSString stringWithFormat:@"%ld", indexPath.section + 1]];
    
    radioCollectionViewCell.radioMdoel = array[indexPath.row];
    
    radioCollectionViewCell.backgroundColor = [UIColor whiteColor];
    return radioCollectionViewCell;
    
}





- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{


    WJYRadioCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
   

    WJYRadioModel *radioM = [[WJYRadioModel alloc]init];

    radioM = self.radioTitleArray[indexPath.section];
    headView.radioModel = self.radioTitleArray[indexPath.section];


    return headView;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    WJYRadioPlayerViewController *radioPlayerVC = [[WJYRadioPlayerViewController alloc]init];
    
    NSMutableArray *array = [self.radioDic objectForKey:[NSString stringWithFormat:@"%ld", indexPath.section + 1]];
    
    radioPlayerVC.tid = [array[indexPath.row] tid];
    [self.navigationController pushViewController:radioPlayerVC animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrameVidio" object:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
